package com.blink.curator.helloworld;

import com.blink.base.BaseTestSample;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.jupiter.api.Test;

/**
 * <Description> 创建连接</Description>
 * <ClassName> CreateSessionSample</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月13日 14:19
 */
public class CreateSessionSample extends BaseTestSample {
    /**
     * <Title>testCreateSession</Title>
     * <Description> 使用curator来创建一个ZooKeeper客户端</Description>
     *
     * @return void
     * @throws
     */
    @Test
    public void testCreateSession() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client =
                CuratorFrameworkFactory.newClient(ROOT_PATH,
                        5000,
                        3000,
                        retryPolicy);
        client.start();
        sleep();
    }

    /**
     * <Title>testCreateSessionFluent</Title>
     * <Description> 使用流式风格的API接口来创建一个ZooKeeper客户端</Description>
     *
     * @return void
     * @throws
     */

    @Test
    public void testCreateSessionFluent() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client =
                CuratorFrameworkFactory.builder()
                        .connectString(ROOT_PATH)
                        .sessionTimeoutMs(5000)
                        .retryPolicy(retryPolicy)
                        .build();
        client.start();
        sleep();
    }

    /**
     * <Title>testCreateSessionFluent</Title>
     * <Description> 使用curator来创建一个含隔离命名空间的ZooKeeper客户端</Description>
     *
     * @return void
     * @throws
     */
    @Test
    public void testCreateSessionWithNamespace() {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client =
                CuratorFrameworkFactory.builder()
                        .connectString(ROOT_PATH)
                        .sessionTimeoutMs(5000)
                        .retryPolicy(retryPolicy)
                        .namespace("base")
                        .build();
        client.start();
        sleep();
    }
}
